<template>
  <a-config-provider :locale="locale">
    <router-view />
    <global-setting />
  </a-config-provider>
</template>

<script lang="ts" setup>
  import { computed, defineComponent, onMounted } from 'vue';
  import enUS from '@arco-design/web-vue/es/locale/lang/en-us';
  import zhCN from '@arco-design/web-vue/es/locale/lang/zh-cn';
  import GlobalSetting from '@/components/global-setting/index.vue';
  import useLocale from '@/hooks/locale';
  import { useAppStore, useUserStore } from '@/store';
  import { useRouter } from 'vue-router';
  import { Message } from '@arco-design/web-vue';

  const { currentLocale } = useLocale();
  const locale = computed(() => {
    switch (currentLocale.value) {
      case 'zh-CN':
        return zhCN;
      case 'en-US':
        return enUS;
      default:
        return enUS;
    }
  });

  const appStore = useAppStore();
  const userStore = useUserStore();
  const router = useRouter();

  const initApp = async () => {
    const token = localStorage.getItem('token');
    if (token && !appStore.menuFromServer) {
      console.log('App初始化: 检测到token但没有菜单数据，开始获取菜单');
      try {
        await appStore.fetchServerMenuConfig();
      } catch (error) {
        console.error('App初始化: 获取菜单失败', error);
        Message.error('获取菜单数据失败，请重新登录');
        router.push({ name: 'login' });
      }
    }
  };

  onMounted(() => {
    initApp();
  });
</script>

